
# R数据分析平台依赖安装脚本
# 运行此脚本安装所有必需的R包

# 设置镜像源（中国用户）
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))

# 定义需要安装的包
packages <- c(
  "shiny",
  "shinydashboard",
  "tidyverse",
  "readxl",
  "DT",
  "echarts4r",
  "cluster",
  "factoextra",
  "corrplot",
  "rmarkdown",
  "kableExtra",
  "ggplot2"
)

# 检查并安装缺失的包
to_install <- packages[!packages %in% installed.packages()[,"Package"]]

if (length(to_install) > 0) {
  message("正在安装缺失的包：", paste(to_install, collapse = ", "))
  install.packages(to_install, dependencies = TRUE)
} else {
  message("所有包都已安装")
}

# 安装TinyTeX用于PDF报告生成
if (!requireNamespace("tinytex", quietly = TRUE)) {
  install.packages("tinytex")
}

# 检查TinyTeX安装
if (!tinytex::is_tinytex()) {
  message("正在安装TinyTeX...")
  tinytex::install_tinytex()
}

message("依赖安装完成！")
message("运行 shiny::runApp('R-data-analysis') 启动应用")
